<?php 
    require_once '../funciones.php';
    $con = con_caweb();
    $versolocambiosabiertos = $_SESSION['caweb']['versolocambiosabiertos'];
    $vercambioscliente = $_SESSION['caweb']['vercambioscliente'];
    $versucursalcambio = $_SESSION['caweb']['versucursalcambio'];

    $condicion="";
    if ($versolocambiosabiertos == 'Si') $condicion.=" AND cont.estado = ''";
    if ($vercambioscliente <> '0') $condicion.=" AND cab.cliente = '$vercambioscliente'";
    if ($versucursalcambio <> '0') $condicion.=" AND cab.sucursalresponsable = '$versucursalcambio'";

    $page = $_REQUEST['page'];

    // get how many rows we want to have into the grid - rowNum parameter in the grid
    $limit = $_REQUEST['rows'];

    // get index row - i.e. user click to sort. At first time sortname parameter -
    // after that the index from colModel
    $sidx = $_REQUEST['sidx'];

    // sorting order - at first time sortorder
    $sord = $_REQUEST['sord'];

    // if we not pass at first time index use the first column for the index or what you want
    if(!$sidx) $sidx =1;

    // calculate the number of rows for the query. We need this for paging the result
    $sql = "select count(cab.pedido) as count
            from cabeceras_ped cab
            inner join contrapartes cont on cont.cambio = cab.pedido
            left join clientes cli on cli.nit = cab.cliente
            where cab.documento = 'CAM' $condicion";
    $result = mysql_query($sql) or die("Error 3234hj".  mysql_error().$sql);
    $row = mysql_fetch_array($result,MYSQL_ASSOC);
    $count = $row['count'];

    // calculate the total pages for the query
    if( $count > 0 ) $total_pages = ceil($count/$limit);
    else $total_pages = 0;

    // if for some reasons the requested page is greater than the total
    // set the requested page to total page
    if ($page > $total_pages) $page=$total_pages;

    // calculate the starting position of the rows
    $start = $limit*$page - $limit;

    // if for some reasons start position is negative set it to 0
    // typical case is that the user type 0 for the requested page
    if($start <0) $start = 0;

    // the actual query for the grid data
    $sql = "select cont.id,
            cab.pedido as cambio,
            cab.fechacomprometida,
            cab.usuariocreo,
            cab.sucursal as sucCreo,
            cab.sucursalresponsable as sucFac,
            cont.presunto,
            cont.itempresunto,
            cont.asignado,
            cont.itemasignado,
            cont.internocruce,
            cont.fechaenvio,
            cab.tipo,
            cab.cliente,
            cli.name,
            cont.estado,
            cab.estado as cabEstado
            from cabeceras_ped cab
            inner join contrapartes cont on cont.cambio = cab.pedido
            left join clientes cli on cli.nit = cab.cliente
            where cab.documento = 'CAM' $condicion ORDER BY $sidx $sord LIMIT $start , $limit";
    $result = mysql_query($sql,$con ) or die("Error 334asdasadfdasd .".mysql_error().$sql);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";

	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
            echo "<row id='". $row['id']."'>";
            echo "<cell>". $row['cambio']."</cell>";
            echo "<cell>". $row['fechacomprometida']."</cell>";
//            echo "<cell>". $row['usuariocreo']."</cell>";
            echo "<cell>". $row['sucCreo']."</cell>";
            echo "<cell>". $row['sucFac']."</cell>";
            echo "<cell>". $row['presunto']."</cell>";
            echo "<cell>". utf8_encode(htmlspecialchars($row['itempresunto']))."</cell>";
            echo "<cell>". $row['asignado']."</cell>";
            echo "<cell>". utf8_encode(htmlspecialchars($row['itemasignado']))."</cell>";
            echo "<cell>". $row['internocruce']."</cell>";
            echo "<cell>". $row['fechaenvio']."</cell>";
            echo "<cell>". strtoupper($row['tipo'])."</cell>";
            echo "<cell>". $row['cliente']."</cell>";
            echo "<cell>". utf8_encode(htmlspecialchars($row['name']))."</cell>";
            echo "<cell>". $row['estado']."</cell>";
            $tmp = ($row['cabEstado'] == "Contraparte")?"Contraparte":"";
            echo "<cell>". $tmp."</cell>";
            echo "</row>";
	}
	echo "</rows>";
	 
?>
